package akka.in.action

import akka.actor.{Actor, ActorLogging}
import akka.cluster.Cluster
import akka.cluster.ClusterEvent._
//import akka.cluster.Reachability.Unreachable
import akka.cluster.protobuf.msg.ClusterMessages.MemberStatus

/**
  * Created by shichen on 2017/7/26.
  */

class ClusterDomainEventListern extends Actor with ActorLogging {
  Cluster(context.system).subscribe(self,classOf[ClusterDomainEvent])

  override def receive: Receive = {
    case MemberUp(member) => log.info(s"$member up")
    case MemberExited(member) => log.info(s"$member exited")
    case MemberRemoved(m,previousStatus) =>
      if (previousStatus == MemberStatus.Exiting) {
        log.info(s"member $m gracefully exited. Removed")
      }else {
        log.info(s"member $m abruptly unreable. Removed")
      }
    case UnreachableMember(m) => log.info(s"$m unreachable")
    case ReachableMember(m) => log.info(s"$m reachable again")
    case s: CurrentClusterState => log.info(s"cluster state:$s")
  }

  override def postStop(): Unit =  {
    Cluster(context.system).unsubscribe(self)
    super.postStop()
  }
}
object ch14_clustering extends App{

}
